<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加图书</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
</head>

<body>
    <h2>添加图书</h2>
    <form>
        <input type="text" name="name" id="name"><br />
        <input type="text" name="author" id="author"><br />
        <input type="number" name="price" id="price"><br />
        <input type="submit" value="提交" id="btn">
    </form>
    <script>
        document.querySelector('#btn').onclick = async (e) => {
            e.preventDefault(); //阻止默认事件
            const name = document.getElementById('name').value
            const author = document.getElementById('author').value
            const price = document.getElementById('price').value
            const {
                data: res
            } = await axios.post('http://localhost:8080/book', {
                name,
                author,
                price
            })
            if (res.code !== 200) return alert(res.message)
            window.location = './list.html'
        }
    </script>
</body>

</html>